package com.diyunapp.xilinapp.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.diyunapp.xilinapp.R;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends Activity implements OnPageChangeListener {

	private ViewPager viewPager;
	private List<View> list;
	private int laststate = 0;
	private int currentp = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);
		viewPager = (ViewPager) findViewById(R.id.splash_vp);
		viewPager.setOnPageChangeListener(this);
		
		int[] imgRes = getImgRes();
		list = new ArrayList<View>();
		for (int i = 0; i < imgRes.length; i++) {
			ImageView iv = new ImageView(this);
			iv.setBackgroundResource(imgRes[i]);
			list.add(iv);
		}
		MyAdapter adapter = new MyAdapter();
		viewPager.setAdapter(adapter);
	}

	private int[] getImgRes() {
		return new int[] { R.drawable.yin, R.drawable.yin1,
				R.drawable.yin2 };
	}
	
	class MyAdapter extends PagerAdapter {

		@Override
		public int getCount() {

			return (list != null ? list.size() : 0);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {

			return (arg0 == arg1);
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {

			container.removeView(list.get(position));
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {

			container.addView(list.get(position));

			return list.get(position);
		}
	}
	
	@Override
	public void onPageSelected(int position) {
		// TODO Auto-generated method stub
		currentp = position;
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		if((currentp == list.size()-1) && laststate == 1 && arg0==0)
		{
			Intent intent = new Intent(GuideActivity.this,
					MainActivity.class);
			startActivity(intent);
			finish();
		}
		else
		{
			laststate = arg0;
		}
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}
}
